/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 9/9/2009
 * Time: 8:47 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using popLib;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace QuickGenForm
{
	class Program
	{
		public static void Main(string[] args)
		{
			initProperties();
			
			Hashtable pro = new Hashtable();
			pro.Add("name","button1");
			pro.Add("text","OK");
			//pro.Add("bgcolor","Red");
			pro.Add("autosize","0");
			pro.Add("enabled","true");
			pro.Add("visible","1");
			//pro.Add("bgimg",@"D:\My Documents\My Pictures\Memoriz\Images\30082009(013).jpg");			
			pro.Add("font","8.5,ITALIC,Arial");
			//pro.Add("padding","15");
			pro.Add("margin","1,2,3,4");
			pro.Add("size","60,25");
			pro.Add("location","10,10");
			
			addControl( FormLib.ControlType.Button, pro );
			
			pro["name"] = "button2";
			pro["text"] = "Cancel";
			pro["location"] = "70,10";
			addControl( FormLib.ControlType.Button, pro );
			
			generateForm( @"D:\My Documents\SharpDevelop Projects\Demo" );
		}
		
		static string PREP_STR = ""; //pre-init code - in main class
		static string INIT_STR = ""; //code to init object - in InitializeComponent function
		static string DESC_STR = ""; //code to set attribute - in InitializeComponent function
		static string FORM_STR = ""; //code to set attribute for main form - in InitializeComponent function
		
		static Hashtable standardProperties;
		public static void initProperties(){
			standardProperties = new Hashtable();
			standardProperties.Add( "backcolor", "BackColor|COLOR" );
			standardProperties.Add( "bgcolor", "BackColor|COLOR" );
			standardProperties.Add( "forecolor", "ForeColor|COLOR" );
			standardProperties.Add( "fcolor", "ForeColor|COLOR" );
			standardProperties.Add( "autosize", "AutoSize|BOOL" );
			standardProperties.Add( "enabled", "Enabled|BOOL" );
			standardProperties.Add( "visible", "Visible|BOOL" );
			standardProperties.Add( "show", "Visible|BOOL" );
			standardProperties.Add( "backgroundimage", "BackgroundImage|IMGFILE" );
			standardProperties.Add( "bgimage", "BackgroundImage|IMGFILE" );
			standardProperties.Add( "bgimg", "BackgroundImage|IMGFILE" );
			standardProperties.Add( "dock", "Dock|DOCKSTYLE" );
			standardProperties.Add( "font", "Font|FONT" );
			standardProperties.Add( "padding", "Padding|PADDING" );
			standardProperties.Add( "margin", "Margin|MARGIN" );
			standardProperties.Add( "size", "Size|SIZE" );
			standardProperties.Add( "maximumsize", "MaximumSize|SIZE" );
			standardProperties.Add( "maxsize", "MaximumSize|SIZE" );
			standardProperties.Add( "minimumsize", "MinimumSize|SIZE" );
			standardProperties.Add( "minsize", "MinimumSize|SIZE" );
			standardProperties.Add( "location", "Location|POINT" );
			standardProperties.Add( "name", "Name|STRING" );
			standardProperties.Add( "text", "Text|STRING" );
			standardProperties.Add( "width", "Width|INT" );
			standardProperties.Add( "height", "Height|INT" );
		}
		
		public static string processValue(string value, string valueType ){
			string returnValue = "";
			switch( valueType ){
				case "IMGFILE": 
					returnValue = "System.Drawing.Image.FromFile(\""+value.Replace("\\","\\\\")+"\")";
					break;
				case "COLOR":
					returnValue = "System.Drawing.Color."+StringLib.capFirstLetter(value.ToLower());
					break;
				case "BOOL":
					returnValue = (value == "1" || value.ToLower() == "true") ? "true" : "false";
					break;
				case "DOCKSTYLE":
					returnValue = "System.Windows.Forms.";
					switch(value.ToLower()){
						case "top": returnValue += "DockStyle.Top"; break;
						case "bottom": returnValue += "DockStyle.Bottom"; break;
						case "left": returnValue += "DockStyle.Left"; break;
						case "right": returnValue += "DockStyle.Right"; break;
						case "fill": returnValue += "DockStyle.Fill"; break;
						case "none": 
						default:
						returnValue += "DockStyle.None"; break;
					}
					
					break;				
				case "FONT":
					string font = "Microsoft Sans Serif";
					string size = "8.25";
					string type = "Regular";
					foreach( string part in StringLib.splitByAny(value, ";,|") ){
						if( ",regular,bold,italic,strikeout,underline,".IndexOf(","+part.ToLower()+",") >= 0 )
							type = StringLib.capFirstLetter(part.ToLower());
						else if( NumberLib.getInt(part) > 0 )
							size = part;
						else font = part;
					}
						
					returnValue = string.Format("new System.Drawing.Font(\"{0}\", {1}F, System.Drawing.FontStyle.{2}, System.Drawing.GraphicsUnit.Point, ((byte)(0)))",
					                           font, size, type);	
					break;
				case "PADDING":
				case "MARGIN":
					string[] parts = StringLib.splitByAny(value,",;|");					
					if( parts.Length == 1 )
						returnValue = "new System.Windows.Forms.Padding("+NumberLib.getInt(value)+")";
					else{
						int left 	= parts.Length > 0 ? NumberLib.getInt(parts[0]) : 0;
						int top		= parts.Length > 1 ? NumberLib.getInt(parts[1]) : 0;
						int right 	= parts.Length > 2 ? NumberLib.getInt(parts[2]) : 0;
						int bottom 	= parts.Length > 3 ? NumberLib.getInt(parts[3]) : 0;
						returnValue = string.Format("new System.Windows.Forms.Padding({0},{1},{2},{3})", left, top, right, bottom);
					}
					break;				 
				case "SIZE":
				case "POINT":
					string[] parts1 = StringLib.splitByAny(value,",;|");
					int a = parts1.Length > 0 ? NumberLib.getInt(parts1[0]) : 0;
					int b = parts1.Length > 1 ? NumberLib.getInt(parts1[1]) : 0;
					returnValue = string.Format( "new System.Drawing."+StringLib.capFirstLetter(valueType.ToLower())+"({0},{1})",
					                           a,b);
					break;				 
				case "STRING": returnValue = string.Format("\"{0}\"",value);	break;
				case "INT": returnValue = ""+NumberLib.getInt(value);	break;
			}
			
			return (returnValue + ";").Replace(";;",";");
		}
		
		static int counter = 0;
		public static void addControl( FormLib.ControlType type, Hashtable properties ){
			string controlType = type.ToString();
			string controlName = properties.ContainsKey("name") ? properties["name"]+"" : controlType+"_"+(counter++);
					
			PREP_STR += string.Format("private System.Windows.Forms.{0} {1};\r\n", controlType, controlName );
			
			INIT_STR += string.Format("\t\t\tthis.{1} = new System.Windows.Forms.{0}();\r\n", controlType, controlName );
			
			DESC_STR += "\t\t\t//\r\n\t\t\t//"+controlName+"\r\n\t\t\t//\r\n";
			
			string[] tmp;
			foreach( string key in properties.Keys ){
				if( standardProperties.ContainsKey(key.ToLower()) ){
					tmp = standardProperties[key].ToString().Split('|');
					DESC_STR += "\t\t\tthis." + controlName + "." + tmp[0] + " = " + processValue( properties[key].ToString(), tmp[1] ) + "\r\n";
				}
			}
			
			FORM_STR += "\t\t\tthis.Controls.Add(this."+controlName+");\r\n";			
		}
		
		static string FORM_NAME = "Form2";
		static string NAMESPACE = "Demo";
		public static void generateForm(string path){
			#region string body = ...
			string body = @"namespace [NAMESPACE]
{
	partial class [FORM_NAME]
	{
		private System.ComponentModel.IContainer components = null;
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		private void InitializeComponent()
		{
[INIT_STR]
			this.SuspendLayout();
[DESC_STR]
			// 
			// [FORM_NAME]
			// 
			this.ClientSize = new System.Drawing.Size(341, 273);
[FORM_STR]
			this.Name = ""[FORM_NAME]"";
			this.Text = ""[FORM_NAME]"";
			this.ResumeLayout(false);
		}
		[PREP_STR]
	}
}";
			#endregion
			
			body = body.Replace("[FORM_NAME]", FORM_NAME);
			body = body.Replace("[NAMESPACE]", NAMESPACE);
			body = body.Replace("[INIT_STR]", INIT_STR);
			body = body.Replace("[DESC_STR]", DESC_STR);
			body = body.Replace("[FORM_STR]", FORM_STR);
			body = body.Replace("[PREP_STR]", PREP_STR);
			
			FileLib.writeFile( Path.Combine( path, FORM_NAME+".Designer.cs"), body );
		}
	
		/***********************************************************************************************************/
		
		public static void  	
	}
}
